#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

#include <conio.h>
#include <unistd.h>
#include <windows.h>

#define INIT_PADLEN 4
#define WIDTH 70
#define HEIGHT 20

void init_map(char [][WIDTH]);
void printout(char [][WIDTH]);
// init the pos struct for the function
struct pos
{
    const int x;
    int y;
};
void board(char [][WIDTH], struct pos ,struct pos);

int main(void)
{
    bool flag = true;
    char map[HEIGHT][WIDTH];
    //init the map
    init_map(map);
    //the main loop

    //init board
    struct pos B1 = {3, 9}, B2 = {67, 9};
    while (flag)
    {
        board(map, B1, B2);
    }
    //the main loop
    return 0;
}



void init_map(char map[][WIDTH])
{
    // init map
    for (int i = 0; i < HEIGHT; i++)
    {
        // init the first&last line
        if (i == 0 || i == HEIGHT - 1)
        {
            for (int j = 0; j < WIDTH; j++)
            {
                map[i][j] = '=';
            }
        }
        else
        {
            // init the wall on left&right
            map[i][0] = '#';
            map[i][WIDTH - 1] = '#';
            for (int j = 1; j < WIDTH - 1; j++)
            {
                map[i][j] = ' ';
            }
        }
    }
    //init map
}


void printout(char map[][WIDTH])
{
    //print out the map
    char *p;
    p = &map[0][0];

    int a = 0;
    while (p < &map[HEIGHT][WIDTH - 1] + 1)
    {
        if (a == WIDTH)
        {
            printf("\n");
            a = 0;
        }
        printf("%c", *p++);
        a++;
    }
    //print out map
}


void board(char map[][WIDTH], struct pos B1, struct pos B2)
{
  CONSOLE_CURSOR_INFO cursor_info = {1, 0}; /*隐藏光标*/
  SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
  init_map(map);
  B1.y = B2.y = 9;

  while (1)
  {
    int save1y, save2y,ctrl; 
    save1y = B1.y;
    save2y = B2.y;
    if (kbhit())
    {
      ctrl = getch();
      if (ctrl == 'w' && B1.y != 2) /*p1：ws上下*/
        B1.y -= 1;                  /*p2：85上下*/
      if (ctrl == 's' && B1.y != 17)
        B1.y += 1;
      if (ctrl == '8' && B2.y != 2)
        B2.y -= 1;
      if (ctrl == '5' && B2.y != 17)
        B2.y += 1;
      ctrl = '0';
    }
 
    for (int i = -1; i != 2; i++)
      map[save1y + i][B1.x] = map[save2y + i][B2.x] = ' '; /*清空原来的板*/
    for (int i = -1; i != 2; i++)
      map[B1.y + i][B1.x] = map[B2.y + i][B2.x] = '|'; /*放上新的板*/

  }
